<style>

</style>
<?php
use yii\helpers\Html;
use yii\helpers\Url;
use yii\widgets\ActiveForm;
use kitt2000\bill\Bill;

/* @var $this yii\web\View */
/* @var $model backend\modules\supplier\models\StorageIn */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="storage-in-form">

    <?php
    $form = ActiveForm::begin([
    ]);
    ?>

    <div class="row">
        <div class="col-sm-2">
            <div class="form-group field-storagein-list_img">
                <label class="control-label" for="storagein-list_img">显示图片</label>
                <div id="storagein-list_img">
                    <label><input type="radio" name="StorageIn[list_img]" value="0" checked="" onclick="listImg(0)"> 不显示</label>
                    <label><input type="radio" name="StorageIn[list_img]" value="1" onclick="listImg(1)"> 显示</label>
                </div>
                <div class="help-block"></div>
            </div>
        </div>
        <div class="col-sm-2"><?= $form->field($model, 'remark')->textInput(['maxlength' => 20]) ?></div>
    </div>

    
    
    <?php
    $setting = [
        'focus'=>'sn',
        'fields' => [
            [
                'header' => '图片',
                'options' => ['class' => 'img none'],
                'content' => [
                    'img' => ['tag' => 'img', 'options' => ['width' => 40, 'height' => 40]],
                ],
            ],
            [
                'header' => '名称',
                'options' => ['class' => 'name'],
                'content' => ['name' => ['tag' => 'text']],
            ],
            [
                'header' => '厂家编码',
                'options' => ['class' => 'sn_purchase'],
                'content' => ['sn_purchase' => ['tag' => 'text']],
            ],
            [
                'header' => '库存',
                'options' => ['class' => 'stock'],
                'content' => ['stock' => ['tag' => 'text']],
            ],
            [
                'header' => '货位',
                'options' => ['class' => 'slot'],
                'content' => ['slot' => ['tag' => 'text']],
            ],
            [
                'header' => '货品编号 <span class="red">*</span>',
                'content' => [
                    'sn' => ['tag' => 'input', 'type' => 'text', 'name' => 'StorageIn[products][sn][]', 'options' => ['class' => 'sn', 'onkeydown' => 'getProductInfo(this);']],
                    'id' => ['tag' => 'input', 'type' => 'hidden', 'name' => 'StorageIn[products][id][]', 'options' => ['class' => 'id']],
                ],
            ],
            [
                'header' => '数量 <span class="red">*</span>',
                'options' => ['class' => 'num'],
                'content' => ['number' => ['tag' => 'input', 'type' => 'text', 'name' => 'StorageIn[products][number][]', 'options' => [
                    'class' => 'number require',
                    'onchange'=>'count(this);',
                    'onkeydown' => 'setPrice(this);'
                    ]]],
            ],
            [
                'header' => '价格 <span class="red">*</span>',
                'content' => ['price' => ['tag' => 'input', 'type' => 'text', 'name' => 'StorageIn[products][price][]', 'options' => [
                    'class' => 'price money require',
                    'onchange'=>'count(this);', 
                    'onkeydown' => 'createNew(this);'
                    ]]],
            ],
            [
                'header' => '小计',
                'options' => ['class' => 'count'],
                'content' => ['count' => ['tag' => 'text']],
            ],
            [
                'header' => '备注',
                'content' => ['remark' => ['tag' => 'input', 'type' => 'text', 'name' => 'StorageIn[products][remark][]']],
            ],
            [
                'header' => '操作',
                'content' => ['del' => ['tag' => 'a', 'text' => '删除', 'url' => 'javascript:void(0);','options'=>['onclick'=>'del(this);']]],
            ],
        ],
    ];

    $nodes = [
        
    ];

//    $nodes = [
//        ['img' => '', 'name' => '', 'number' => '', 'price' => '', 'stock' => '', 'sn' => '','sn_purchase' => '', 'remark' => '', 'slot' => '', 'price' => ''],
//        ['img' => '', 'name' => '', 'number' => '', 'price' => '', 'stock' => '', 'sn' => '','sn_purchase' => '', 'remark' => '', 'slot' => '', 'price' => ''],
//        ['img' => '', 'name' => '', 'number' => '', 'price' => '', 'stock' => '', 'sn' => '','sn_purchase' => '','remark' => '', 'slot' => '', 'price' => ''],
//    ];

    echo Bill::widget(['id' => 'bill', 'setting' => $setting, 'nodes' => $nodes]);
    ?>

   

    <div class="form-group">
        <?= Html::button($model->isNewRecord ? '新建' : '更新', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary','onclick'=>"$('#{$form->getId()}').submit();"]) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>

<script>
    window.onload = function () { 
        form_beforeSubmit();
        format_init();
        del_onclick();
    };
    
    function del_onclick(){
        $('#bill .del').click(function(){
            alert('del');
        });
    }

    function listImg(type) {
        if (type == 1) {
            $('#bill .img').removeClass('none');
        } else {
            $('#bill .img').addClass('none');
        }
    }

    function form_beforeSubmit() {
        $('#<?= $form->getId() ?>').on('beforeSubmit', function () {
            var isBreak = false;
            
            $('#<?= $form->getId() ?> tr.item').each(function(){
                if($(this).find('.sn')==''){
                    $(this).remove();
                }
                
                $(this).find('.require').each(function(){
                    if ($.trim($(this).val()) == '') {
                        alert('必填项为空！');
                        $(this).focus();
                        isBreak = true;
                    }
                });
            });
            
            return isBreak == false ? true : false;
        });
    }
    
    function getProductInfo(e){
        if (event.keyCode != 13){
            return false;
        }
        
        var val=$.trim($(e).val());
        if(val!==''){
            
            //检查是否重复
            var isBreak=false;
            $('#bill .sn').each(function(){
                if(e!==this && val==$(this).val()){
                    $(this).focus();
                    $(e).val('');
                    isBreak=true;
                }
            });
            
            if(isBreak==true){
                return false;
            }
            
            var url='<?=  Url::to(['get-product-by-sn']);?>';
            $.getJSON(url,{'sn':val},function(ret){
                if(ret.result==false){
                    $(e).val('');
                    alert(ret.content.msg);
                }else{
                    var tr=$(e).parent().parent();
                    setProductInfo(tr,ret.content.product);
                    $(tr).find('.number').focus();
                }
            });
        }
    }
    
    function setProductInfo(tr,info){
        $(tr).find('img.img').attr('src',info.img);
        $(tr).find('td.name').html(info.name);
        $(tr).find('td.sn_purchase').html(info.sn_purchase);
        $(tr).find('td.stock').html(info.stock);
        $(tr).find('td.slot').html(info.slot);
        $(tr).find('.price').val(info.price);
    }
    
    function setPrice(e){
        if (event.keyCode == 13){
            var tr=$(e).parent().parent();
            $(tr).find('.price').select();
            count(e);
        }
    }
    
    function createNew(e){
        event.keyCode == 13 && newRow(e);
    }
    
    function newRow(e){
        var bill = $.fn.bill.getObj("bill");
        bill && bill.newRow();
        count(e);
        format_init();
    }
    
    function del(e){
        var tr=$(e).parent().parent();
        tr.remove();
        totalCount();
        var bill = $.fn.bill.getObj("bill");
        bill && bill.serial();
    }
    
    function count(e){
        var tr=$(e).parent().parent();
        var price=parseFloat($(tr).find('.price').val());
        var number=parseInt($(tr).find('.number').val());
        $(tr).find('.count').html(price*number);
        
        totalCount();
    }
    
    function totalCount(){
        var countNum=allCount=0;
        $('#bill tr.item').each(function(){
            var number=$(this).find('.number').val()=='' ? 0:parseInt($(this).find('.number').val());
            var price=$(this).find('.price').val()=='' ? 0: parseFloat($(this).find('.price').val());
            countNum+=number;
            allCount+=number*price;
        });

        $('#bill').find('.all-count .num').html(countNum);
        $('#bill').find('.all-count .count').html(allCount);
    }
    
</script>
